package com.example.blueconn.data;

import androidx.lifecycle.MutableLiveData;

public class DeviceItemData implements ItemData {
    private final MutableLiveData<Boolean> clearData = new MutableLiveData<>();
    private final MutableLiveData<Boolean> isOnline = new MutableLiveData<>(false);
    private final MutableLiveData<Boolean> isDelete = new MutableLiveData<>();


    private final MutableLiveData<Integer> charge = new MutableLiveData<>();
    private final MutableLiveData<String> tempData = new MutableLiveData<>();
    private final MutableLiveData<String> lightData = new MutableLiveData<>();
    private final MutableLiveData<Integer> deviceNum = new MutableLiveData<>(-1);

    private DataBean dataBean;

    private int preCharge = 0;

    public DeviceItemData(DataBean dataBean) {
        this.dataBean = dataBean;
        deviceNum.setValue(dataBean.getDeviceNum());
        tempData.setValue(dataBean.getTempData());
        if (dataBean.getDeviceNum() != 0) {
            lightData.setValue(String.valueOf(dataBean.getLightInt()));
        } else {
            lightData.setValue(dataBean.getLOrHData());
        }
        preCharge = dataBean.getCharge();
        charge.setValue(dataBean.getCharge());
    }

    public void updateData(DataBean dataBean) {
        this.dataBean = dataBean;
        tempData.setValue(dataBean.getTempData());
        if (dataBean.getDeviceNum() != 0) {
            lightData.setValue(String.valueOf(dataBean.getLightInt()));
        } else {
            lightData.setValue(dataBean.getLOrHData());
        }
        if (preCharge != dataBean.getCharge()) {
            charge.setValue(dataBean.getCharge());
            preCharge = dataBean.getCharge();
        }
        if (!isOnline.getValue()) {
            isOnline.setValue(true);
        }
    }

    @Override
    public MutableLiveData<Integer> getCharge() {
        return charge;
    }

    @Override
    public MutableLiveData<Boolean> getOnlineState() {
        return isOnline;
    }

    @Override
    public MutableLiveData<Boolean> getDelOpr() {
        return isDelete;
    }

    @Override
    public MutableLiveData<Boolean> getClearState() {
        return clearData;
    }

    @Override
    public MutableLiveData<Integer> getDeviceNumber() {
        return deviceNum;
    }

    @Override
    public MutableLiveData<String> getTempData() {
        return tempData;
    }

    @Override
    public MutableLiveData<String> getLightData() {
        return lightData;
    }

    public int getDeviceNumb() {
        return dataBean == null ? -1 : dataBean.getDeviceNum();
    }

    public void setIsOnline(boolean isOnline) {
        this.isOnline.postValue(isOnline);
    }
}
